import java.util.Random;
import java.util.Vector;
import java.util.Collections;

//handles adding new things to animalhandler
//called by mating handler

@SuppressWarnings("unused")
public class BirthHandler {
	public static boolean DEBUG = false;
	public static void setDEBUG(boolean b) {DEBUG = b;}

	//create the animal
	//add the animal to the animalhandlerobject
	//start up the behaviors

	static Random R = new Random();
	// false = female, true = male. 
	//boolean to save overhead
	//as in nature, animals default to female

	//AnimalHandlerObject.cloneCarnivore();

	public static void doBirthingSpawn(AnimalObject father, AnimalObject mother) {
		//this is where all the cruft of init goes
		//i probably wont comment this since its
		//mostly part of the default animal object
		//seting vars in bad fashion, no reason not to
		AnimalObject me = new AnimalObject();

		me.setType(mother.getType());
		me.setSex(R.nextBoolean()); //random 0 or 1;
		me.adjustOffPow(father.getOffPow()); //offpow bonus
		me.adjustDefPow(father.getDefPow()); //defpow bonus
		me.adjustArea(2); //+3 visible area

		me.setLiving(true);
		me.setHealth(mother.getHealth()); //our health should be something
		me.setAge(0);
		me.setMass(mother.getMass() / 10); //mom was REALLY preggers
		mother.setMass((int)(mother.getMass() * 0.9));
		me.setLocation( mother.getLocation() );
		me.setDisposition(2);
		//ryle: beliefs are just dispositions to behave
		//-paraphrase
		me.setMateable(100); //pedo is bad
		me.setFocus(mother); //we are fixated, yup

		//so.. prepoulate the family vector with mom and dad
		//social handler will take over in a bit.

		/*
		me.addFamilyObject(mother);
		me.addFamilyObject(father);
		for(int l = 0; l < mother.getFamilyObjects().size(); l++) {
			if(me.getFamilyObjects().indexOf(mother.getFamilyObjects().elementAt(l)) >=0)
				me.addFamilyObject(mother.getFamilyObjects().elementAt(l));
		}
		for(int l = 0; l < mother.getFamilyObjects().size(); l++) {
			if(me.getFamilyObjects().indexOf(mother.getFamilyObjects().elementAt(l)) >=0)
				me.addFamilyObject(mother.getFamilyObjects().elementAt(l));
		}
		 */
		//might be useful later


		me.setEnergy(mother.getEnergy() / 2); //birth can kill
		mother.setEnergy(mother.getEnergy() /2);

		AnimalHandlerObject.AddAnimalToHandler(me);	
		if(DEBUG) {
			System.out.println("!!!!!!!!!!!!!!SPAWNED!!!!"+me.getLocation().x+","+me.getLocation().y);
		}
		
		if(doMutation() > 950){
			if(DEBUG)
				System.out.println("MUTANT! KILL IT WITH FIRE");
			int count = doMutationCount();
			switch(count) {
			case 0:
				me.adjustMass(5*doMutationAmount()/4);
				if(DEBUG)
					System.out.println(me.getMass());
				if(R.nextBoolean())
					break;
			case 1:
				me.adjustOffPow(doMutationAmount());
				if(DEBUG)
					System.out.println("OffPow");
				if(R.nextBoolean())
					break;
			case 2:
				me.adjustDefPow(doMutationAmount());
				if(DEBUG)
					System.out.println("DefPow");
				if(R.nextBoolean())
					break;
			case 3:
				if(me.getType())
					me.setType(false);
				else
					me.setType(true);
				if(DEBUG)
					System.out.println("Swap type");
				if(R.nextBoolean())
					break;
			case 4:
				me.adjustArea(doMutationAmount());
				if(DEBUG)
					System.out.println("see further");
				if(R.nextBoolean())
					break;
			case 5:
				me.adjustEnergy(2*doMutationAmount());
				if(DEBUG)
					System.out.println("double energy");
				if(R.nextBoolean())
					break;
			case 6:
				me.adjustHealth(10*doMutationAmount());
				if(DEBUG)
					System.out.println("10*health");
				if(R.nextBoolean())
					break;
			case 7:
				me.setMateable(0);
				if(DEBUG)
					System.out.println("instamate");
				if(R.nextBoolean())
					break;
			case 8:
				me.setFleeing(true);
				me.setDisposition(0);
				if(DEBUG)
					System.out.println("FLEE");
				break;
			case 9:
				me.setBorrowedTime(100);
				if(DEBUG)
					System.out.println("insta starve");
				break;
			default:
				break;
			}

		}
	}

	public static int doMutation() {
		Random R = new Random();
		return R.nextInt(1000);
	}
	public static int doMutationCount() {
		Random R = new Random();
		return R.nextInt(10);
	}
	public static int doMutationAmount() {
		Random R = new Random();
		return R.nextInt(20);
	}

	public static void budding(AnimalObject me) {
		if(DEBUG)
			System.out.println("Mass :"+me.getMass());
			
		AnimalObject myself = new AnimalObject();
		myself = me;
		me.setMass(me.getMass()/2);
		myself.setMass(me.getMass()/2);

		AnimalHandlerObject.AddAnimalToHandler(myself);
	}
	
}
